var Edit = document.getElementsByClassName("link-edit"),
    Delete = document.getElementsByClassName("link-delete"),
    Title = document.querySelectorAll(".address-title"),
    OK = document.getElementsByClassName("link-confirm"),
    btnadd = document.getElementById("add"),
    addressBox = document.querySelectorAll(".address-item"),
    uidnode = document.getElementsByClassName("page-wrap"),
    Detail = document.querySelectorAll(".address-detail");

var Apply= document.getElementById("Apply");

btnadd.setAttribute("tabindex","1");
Apply.style.display = "none";

for(var i = 0;i < addressBox.length;++i){
    addressBox[i].onclick = function (){
        addressBox = document.querySelectorAll(".address-item");
        addressBox = document.querySelectorAll(".address-item");
        for(var j = 0;j < addressBox.length;++j){
            addressBox[j].style.border = "2px dashed #ccc";
            addressBox[j].style.backgroundColor = "#FFFFFF";
        }
        this.style.border = "2px dashed #215496";
        this.style.backgroundColor = "#fae4ea";
    }
}

for(var i = 0;i < Edit.length;++i){
    //console.log("avb");
    Edit[i].onclick = function (){
        this.style.display = "none";
        this.nextElementSibling.style.display = "none";
        this.nextElementSibling.nextElementSibling.style.visibility = "visible";

        this.parentElement.previousElementSibling.setAttribute('contenteditable','true');
        this.parentElement.previousElementSibling.previousElementSibling.setAttribute('contenteditable','true');

        this.parentElement.previousElementSibling.setAttribute("tabindex","1");

        this.parentElement.previousElementSibling.previousElementSibling.setAttribute("tabindex","1");

        this.parentElement.previousElementSibling.previousElementSibling.focus();

    }
}


for(var i = 0;i < OK.length;++i){
    console.log("binding")
    OK[i].onclick = function (){
        console.log("onclick")
        var tt = this.parentElement.previousElementSibling.previousElementSibling,
            dd = this.parentElement.previousElementSibling;
        if(tt.innerHTML !== "" && dd.innerHTML !== ""){
            console.log("添加")
            addAddress(tt.innerHTML,dd.innerHTML,this.parentNode.getAttribute("id"));
        }
        this.style.visibility = "hidden";
        this.previousElementSibling.style.display = "inline";
        this.previousElementSibling.previousElementSibling.style.display = "inline";
        this.parentElement.previousElementSibling.setAttribute('contenteditable','false');
        this.parentElement.previousElementSibling.previousElementSibling.setAttribute('contenteditable','false');
    }
}

for(var i = 0;i < Delete.length;++i){
    Delete[i].onclick = function (){
        var pa = this.parentNode.parentNode.parentNode;
        var tt = this.parentElement.previousElementSibling.previousElementSibling,dd = this.parentElement.previousElementSibling;
        if(tt.innerHTML !== "" && dd.innerHTML !== "") {
            deleteAddress(tt.innerHTML, dd.innerHTML,this.parentNode.getAttribute("id"));
        }
        pa.removeChild(this.parentNode.parentNode);

    }
}

btnadd.onclick = function (){
    console.log("sdasd");

    Edit = document.getElementsByClassName("link-edit"),
        Delete = document.getElementsByClassName("link-delete"),
        Title = document.querySelectorAll(".address-title"),
        OK = document.getElementsByClassName("link-confirm"),
        btnadd = document.getElementById("add"),
        addressBox = document.querySelectorAll(".address-item"),
        uidnode = document.getElementsByClassName("page-wrap"),
        Detail = document.querySelectorAll(".address-detail");

    var div1 = document.createElement("div"),
        div2 = document.createElement("div"),
        div3 = document.createElement("div"),
        div4 = document.createElement("div"),
        divfa = document.getElementById("address-fa"),
        span1 = document.createElement("span"),
        span2 = document.createElement("span"),
        span3 = document.createElement("span");


    div1.setAttribute("class","address-item active");
    div2.setAttribute("class","address-title");
    div3.setAttribute("class","address-detail");
    div4.setAttribute("class","address-operator");
    div4.setAttribute("id","waiting")

    span1.setAttribute("class","link-edit");
    span2.setAttribute("class","link-delete");
    span3.setAttribute("class","link-confirm");

    span1.innerHTML = "编辑";
    span2.innerHTML = "删除";
    span3.innerHTML = "确定";

    div4.appendChild(span1);
    div4.appendChild(span2);
    div4.appendChild(span3);

    div1.appendChild(div2);
    div1.appendChild(div3);
    div1.appendChild(div4);
    divfa.appendChild(div1);

    span1.onclick = function (){
        // var tt = this.parentElement.previousElementSibling.previousElementSibling,dd = this.parentElement.previousElementSibling;
        // if(tt.innerHTML !== "" && dd.innerHTML !== ""){
        //     addAddress(tt.innerHTML,dd.innerHTML,this.parentNode.getAttribute("id"));
        // }
        this.style.display = "none";
        this.nextElementSibling.style.display = "none";
        this.nextElementSibling.nextElementSibling.style.visibility = "visible";
        this.parentElement.previousElementSibling.setAttribute('contenteditable','true');
        this.parentElement.previousElementSibling.previousElementSibling.setAttribute('contenteditable','true');
        this.parentElement.previousElementSibling.setAttribute("tabindex","1");
        this.parentElement.previousElementSibling.previousElementSibling.setAttribute("tabindex","1");
        this.parentElement.previousElementSibling.previousElementSibling.focus();
    }
    span2.onclick = function (){
        var tt = this.parentElement.previousElementSibling.previousElementSibling,dd = this.parentElement.previousElementSibling;
        if(tt.innerHTML != "" && dd.innerHTML != "") {
            deleteAddress(tt.innerHTML, dd.innerHTML,this.parentNode.getAttribute("id"));
        }
        var pa = this.parentNode.parentNode.parentNode;
        console.log(pa);
        pa.removeChild(this.parentNode.parentNode);
    }
    span3.onclick = function (){
        var tt = this.parentElement.previousElementSibling.previousElementSibling,dd = this.parentElement.previousElementSibling;
        if(tt.innerHTML !== "" && dd.innerHTML !== ""){
            addAddress(tt.innerHTML,dd.innerHTML,this.parentNode.getAttribute("id"));
        }
        this.style.visibility = "hidden";
        this.previousElementSibling.style.display = "inline";
        this.previousElementSibling.previousElementSibling.style.display = "inline";
        this.parentElement.previousElementSibling.setAttribute('contenteditable','false');
        this.parentElement.previousElementSibling.previousElementSibling.setAttribute('contenteditable','false');
    }
    div1.onclick = function (){
        addressBox = document.querySelectorAll(".address-item");
        for(var j = 0;j < addressBox.length;++j){
            addressBox[j].style.border = "2px dashed #ccc";
            addressBox[j].style.backgroundColor = "#ffffff";
        }
        this.style.border = "2px dashed #215496";
        this.style.backgroundColor = "#fae4ea";
    }





    console.log(btnadd.parentElement.className);
    console.log(divfa.className);

    divfa.insertBefore(div1,btnadd);

    span1.onclick();

}


function deleteAddress(first,second,locationId){
    console.log("deleting")
    console.log(locationId);
    var uid = uidnode[0].id;
    $.ajax(
        {
            type: 'POST',
            url: 'http://localhost:8080/petstore/address?uid=' + uid + '&lid=' + locationId + '&type=0',
            success: function (data) {

            },
            error: function (errorMsg) {
                console.log("fail")
                console.log(errorMsg);
            },
        })
}
function addAddress(first,second,locationId){
    console.log("locationId: "+locationId)
    var uid = uidnode[0].id;
    $.ajax(
        {
            type: 'POST',
            url: 'http://localhost:8080/petstore/address?uid=' + uid + '&Fs=' + first + '&Ss='
                + second + '&lid=' + locationId +'&type=1',
            success: function (data) {
                var div = document.getElementById("waiting");
                if(div !== null)
                {
                    div.setAttribute("id",data);
                }
            },
            error: function (errorMsg) {
                console.log("fail")
                console.log(errorMsg);
            },
        })
}



var userN = document.querySelectorAll(".panel");

var username=userN[0].id;//用户名
var password=document.getElementById("password");//密码
var tips=document.getElementById("showMessage");//提示信息
var isPasswordValid=false;
//****

//添加监听器区域
password.addEventListener("blur",checkPasswordValid,false);
//****
//检查用户名是否合法：先判空，不为空的话判是否存在

function checkPasswordValid(){
    if(password.value == '' || password.value == null || password.value == undefined){
        tips.innerHTML='';
        tips.innerHTML+='<h4 style="line-height:25px ;color: black;">密码不能为空！</h4>';
        isPasswordValid=false;
        return isPasswordValid;

    }else{
        console.log(username + " " + password.value);
        $.ajax(
            {
                type: 'POST',
                url: 'http://localhost:8080/petstore/check?username=' + username + '&password=' + password.value,

                success: function (data) {
                    var dataJson = JSON.parse(data);
                    console.log(dataJson);
                    if(dataJson & 1){
                        tips.innerHTML='';
                        tips.innerHTML+='<h4 style="line-height:25px ;color: black;">密码正确!</h4>';
                        $("#Apply").attr('disabled',true);
                        Apply.style.display = "inline"
                    }
                    else{
                        tips.innerHTML='';
                        tips.innerHTML+='<h4 style="line-height:25px ;color: black;">密码错误!</h4>';
                        Apply.style.display = "none";
                    }
                    isPasswordValid = dataJson;
                    return isPasswordValid;
                },
                error: function (errorMsg) {
                    console.log("fail")
                    console.log(errorMsg);
                    return false;
                },
            })
    }
}
//点击注册按钮




